package com.betty.cscams.controller;

import java.util.List;

import com.betty.cscams.common.http.HttpResult;
import com.betty.cscams.common.page.PageRequest;
import com.betty.cscams.constant.SysConstants;
import com.betty.cscams.entity.BusTerm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.betty.cscams.service.BusTermService;

/**
 * ---------------------------
 *  (BusTermController)         
 * ---------------------------
 * 时间：  2022-03-12 09:20:50
 * ---------------------------
 */
@RestController
@RequestMapping("busTerm")
public class BusTermController {

	@Autowired
	private BusTermService busTermService;

	/**
	 * 保存
	 * @param record
	 * @return
	 */	
	@PostMapping(value="/save")
	public HttpResult save(@RequestBody BusTerm record) {
		if(record.getStatus() != null && record.getStatus() !=1) {
			return HttpResult.error("该学期已启动，不可修改");
		}
		return HttpResult.ok(busTermService.save(record));
	}

    /**
     * 删除
     * @param records
     * @return
     */
	@PostMapping(value="/delete")
	public HttpResult delete(@RequestBody List<BusTerm> records) {
		for (BusTerm busTerm: records) {
			BusTerm term = busTermService.findById(busTerm.getId());
			if(term.getStatus() != 1){
				return HttpResult.error("请选择未开始的学期进行删除");
			}
		}
		return HttpResult.ok(busTermService.delete(records));
	}

    /**
     * 基础分页查询
     * @param pageRequest
     * @return
     */    
	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody PageRequest pageRequest) {
		return HttpResult.ok(busTermService.findPage(pageRequest));
	}
	
    /**
     * 根据主键查询
     * @param id
     * @return
     */ 	
	@GetMapping(value="/findById")
	public HttpResult findById(@RequestParam Long id) {
		return HttpResult.ok(busTermService.findById(id));
	}

	/**
	 * 开启学期综测填报
	 * @param id
	 * @return
	 */
	@GetMapping(value="/startTerm")
	public HttpResult startTerm(@RequestParam Long id) {
		BusTerm term = busTermService.findById(id);
		if(term.getStatus() != 1){
			return HttpResult.error("请选择未开始的学期进启动");
		}
		BusTerm busTerm = new BusTerm();
		busTerm.setId(id);
		busTerm.setStatus(SysConstants.TERM_STATUS_2);
		return HttpResult.ok(busTermService.updateStatus(busTerm));
	}

	/**
	 * 结束学期综测填报
	 * @param id
	 * @return
	 */
	@GetMapping(value="/endTerm")
	public HttpResult endTerm(@RequestParam Long id) {
		BusTerm term = busTermService.findById(id);
		if(term.getStatus() != 2){
			return HttpResult.error("请选择进行中的学期进结束");
		}
		BusTerm busTerm = new BusTerm();
		busTerm.setId(id);
		busTerm.setStatus(SysConstants.TERM_STATUS_3);
		return HttpResult.ok(busTermService.updateStatus(busTerm));
	}

	/**
	 * 查询填报中的学期
	 * @return
	 */
	@GetMapping(value="/findOptions")
	public HttpResult findOptions(@RequestParam int type) {
		return HttpResult.ok(busTermService.findOptions(type));
	}
	/**
	 * 查询填报中的学期
	 * @return
	 */
	@GetMapping(value="/findCurrentTerm")
	public HttpResult findCurrentTerm() {
		return HttpResult.ok(busTermService.findCurrentTerm());
	}
}
